package tmt.usercenter.web.domain;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.tmt.annotation.*;
import com.tmt.jpa.jpaservice.SyncUpdate;
import com.tmt.jpafieldvalueconvert.JpaFieldValueConvert;
import com.tmt.jpafieldvalueconvert.JpaFieldValueConvertListener;
import com.tmt.jpafieldvalueconvert.impl.FileUrlFieldConverter;
import com.voodoodyne.jackson.jsog.JSOGGenerator;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import tmt.usercenter.web.domain.converter.StringAndEnumListConverter;
import tmt.usercenter.web.domain.converter.StringAndListConverter;
import tmt.usercenter.web.domain.enums.AuthorizedGrantType;
import tmt.usercenter.web.domain.enums.ClientStatus;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * 客户系统，即授权使用本用户中心的第三方系统。
 */
@Getter
@Setter
@Entity
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
@EntityListeners({JpaFieldValueConvertListener.class, AuditingEntityListener.class})
@JsonIdentityInfo(generator = JSOGGenerator.class)

@LazyLoadDefine({
		@LazyLoadMapping(name = "1", value = {"tmtAuthorities"})
})
public class ClientSystem extends BaseCreator implements Serializable {

	/**
	 * 系统名称
	 */
	@Select2OptionItem
	@Column(name = "name", nullable = false, unique = true)
	private String name;

	/**
	 * 系统LOGO图标
	 */
	@JpaFieldValueConvert(converter = FileUrlFieldConverter.class, displayName = "客户LOGO")
	@SyncLoadFile
	@Select2OptionAttr("logo")
	@Column(name = "logo", nullable = false)
	private String logo;

	/**
	 * 用户中心授予的客户ID
	 */
	@Select2OptionItem(order = 1)
	@Column(name = "client_Id", nullable = false, unique = true)
	private String clientId;

	/**
	 * 用户中心授予的客户密钥
	 */
	@Column(name = "client_secret", nullable = false)
	private String clientSecret;

	private String description;

	/**
	 * 是否要求客户密钥
	 */
	@Column(name = "secret_required")
	private boolean secretRequired = true;

	/**
	 * 核准状态
	 */
	@Enumerated(EnumType.STRING)
	private ClientStatus status = ClientStatus.APPLYING;

	/**
	 * 许可操作范围。
	 */
	@Select2OptionValue("scope")
	@Convert(converter = StringAndListConverter.class)
	@Column(name = "scope", nullable = false)
	private List<String> scope;

	/**
	 * 是否进行范围控制
	 */
	@Column(name = "scoped")
	private boolean scoped = false;

	/**
	 * 用户中心的联系邮箱
	 */
	@Column(name = "email", nullable = false)
	private String email;

	/**
	 * 自动批准
	 */
	@Column(name = "auto_approve")
	private boolean autoApprove = true;

	/**
	 * 登录后重定向URL地址。
	 */
	@Convert(converter = StringAndListConverter.class)
	@Column(name = "registered_redirect_uri", nullable = false)
	private List<String> registeredRedirectUri;

	/**
	 * 资源ID。
	 */
	@Select2OptionValue("resourceIds")
	@Convert(converter = StringAndListConverter.class)
	@Column(name = "resource_ids", nullable = false)
	private List<String> resourceIds;

	/**
	 * 授权类型
	 */
	@Select2OptionValue("authorizedGrantTypes")
	@Convert(converter = StringAndEnumListConverter.class)
	@Column(name = "authorized_grant_types", nullable = false)
	private List<AuthorizedGrantType> authorizedGrantTypes;

	/**
	 * 访问令牌有效时间，单位：秒。
	 */
	@Column(name = "access_token_validity_seconds")
	private int accessTokenValiditySeconds = 3600;

	/**
	 * 刷新令牌有效时间，单位：秒。
	 */
	@Column(name = "refresh_token_validity_seconds")
	private int refreshTokenValiditySeconds = 7200;

	/**
	 * 用户信息变更后通知的URL地址。
	 */
	@Column(name = "changed_notify_url")
	private String changedNotifyUrl;

	/**
	 * 用户登出通知的URL地址。
	 */
	@Column(name = "logout_notify_url", nullable = false)
	private String logoutNotifyUrl;

	/**
	 * 客户系统的App下载地址。
	 */
	@Column
	private String appDownloadUrl;

	/**
	 * 授权发送短信的总条数。
	 */
	private Long smTotals= 0L;

	/**
	 * 已经发送短信的总条数。
	 */
	private Long sendTotals = 0L;

	/**
	 * 客户系统的用户注册配置
	 */
	@OneToOne(mappedBy = "originSystem")
	private UserInfoConfig userRegisterConfig;

	/**
	 * 客户系统的所有用户
	 */
	@OneToMany(mappedBy = "originSystem", cascade = {CascadeType.REFRESH})
	private List<TmtUser> allUsers;

	/**
	 * 客户系统的所有用户
	 */
	@OneToMany(mappedBy = "originSystem", cascade = {CascadeType.REFRESH})
	private List<TmtAuthority> tmtAuthorities;

	/**
	 * 客户系统的所有用户
	 */
	@OneToMany(mappedBy = "originSystem", cascade = {CascadeType.REFRESH})
	private List<TmtRole> tmtRoles;


	/**
	 * 用户对客户端的授权列表。
	 */
	@OneToMany(mappedBy = "originSystem", cascade = {CascadeType.REFRESH})
	private List<TmtApproval> tmtApprovals;

	/**
	 * 客户系统发送的短信记录。
	 */
	@OneToMany(mappedBy = "originSystem", cascade = {CascadeType.REFRESH})
	private List<SmHistory> smHistories;

	/**
	 * 客户系统可授权访问的范围集合。
	 */
	@SyncUpdate
	@Select2OptionValue("clientSystemScopes[].authScope.id")
	@OneToMany(mappedBy = "clientSystem", cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
	private List<ClientSystemScope> clientSystemScopes;

	/**
	 * 客户系统可授权访问的资源集合。
	 */
	@SyncUpdate
	@Select2OptionValue("clientSystemResources[].authResource.id")
	@OneToMany(mappedBy = "clientSystem", cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
	private List<ClientSystemResource> clientSystemResources;


}
